package com.library.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class BookStatusTag extends SimpleTagSupport {

    // 属性：图书状态
    private String status;

    // 设置图书状态
    public void setStatus(String status) {
        this.status = status;
    }

    // 创建状态和颜色的映射关系
    private static final Map<String, String> statusColorMap = new HashMap<>();

    static {
        statusColorMap.put("PENDING", "badge-warning"); // 黄色
        statusColorMap.put("APPROVED", "badge-success"); // 绿色
        statusColorMap.put("REJECTED", "badge-danger"); // 红色
        statusColorMap.put("RETURNED", "badge-primary"); // 蓝色
    }

    @Override
    public void doTag() throws JspException, IOException {
        PageContext pc = (PageContext) getJspContext();
        JspWriter out = pc.getOut();

        // 默认badge类
        String badgeClass = "badge ";

        // 获取状态对应的颜色类，如果没有对应的状态则使用默认灰色
        String badgeColor = statusColorMap.getOrDefault(status, "badge-secondary");

        // 拼接完整的badge类
        badgeClass += badgeColor;

        // 输出带有相应颜色的Bootstrap Badge标签
        out.print("<span class=\"" + badgeClass + "\">" + status + "</span>");
    }
}